package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//希尔排序
public class ShellSort {

    public static void main(String[] args) {
//        int arr[] = {2,8,1,5,3};
//        shellSort(arr);
//        System.out.println(Arrays.toString(arr));
        int arr[] = new int[80000];
        for (int i=0;i<80000;i++){
            arr[i] = (int) (Math.random() * 8000000);
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
        String format = simpleDateFormat.format(date1);
        System.out.println("遍历前的时间"+format);
        shellSort(arr);
        Date date2 = new Date();
        String format1 = simpleDateFormat.format(date2);
        System.out.println("遍历后的时间"+format1);
    }

    public static void shellSort(int arr[]){
        int gap = 5;
        int temp = 0;
        int num = 0;
        for (int i=arr.length/gap;i>0;i/=gap){
            //将i点作为初始点，后续点通过i++,i-=gap，将数据找到并分组出来
            for (int j=i;j<arr.length;j++){
                temp = arr[j];
                num = j;
                //组内数据间相互比较
                while (num-i>=0 && temp <arr[num-i]){
                    arr[num] = arr[num-i];
                    num = num-i;
                }
                arr[num] = temp;
            }
        }
    }
}
